package cn.lrwjz.androidguide.ui;

import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import com.google.android.material.button.MaterialButton;
import com.google.android.material.sidesheet.SideSheetBehavior;
import com.google.android.material.sidesheet.SideSheetCallback;
import com.google.android.material.sidesheet.SideSheetDialog;

import cn.lrwjz.androidguide.R;

public class SideSheetActivity extends AppCompatActivity {
    private SideSheetBehavior<FrameLayout> standardSideSheetBehavior;
    private Button btnToggleStandard;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_side_sheet);

        // 设置 Toolbar
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        if (getSupportActionBar() != null) {
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
            getSupportActionBar().setTitle("SideSheet");
        }

        // 初始化标准 SideSheet
        FrameLayout standardSideSheet = findViewById(R.id.standardSideSheet);
        standardSideSheetBehavior = SideSheetBehavior.from(standardSideSheet);
        standardSideSheetBehavior.setState(SideSheetBehavior.STATE_HIDDEN);

        // 设置 SideSheet 回调
        standardSideSheetBehavior.addCallback(new SideSheetCallback() {
            @Override
            public void onStateChanged(@NonNull View sheet, int newState) {
                String state = "";
                switch (newState) {
                    case SideSheetBehavior.STATE_EXPANDED:
                        state = "展开";
                        break;
                    case SideSheetBehavior.STATE_DRAGGING:
                        state = "拖动中";
                        break;
                    case SideSheetBehavior.STATE_SETTLING:
                        state = "固定中";
                        break;
                    case SideSheetBehavior.STATE_HIDDEN:
                        state = "隐藏";
                        break;
                }
                Toast.makeText(SideSheetActivity.this, "SideSheet 状态: " + state, Toast.LENGTH_SHORT).show();
                updateToggleButtonText();
            }

            @Override
            public void onSlide(@NonNull View sheet, float slideOffset) {
                // 可以在这里处理滑动过程中的动画
            }

        });

        // 设置标准 SideSheet 的切换按钮
        btnToggleStandard = findViewById(R.id.btnToggleStandard);
        btnToggleStandard.setOnClickListener(v -> toggleStandardSideSheet());
        updateToggleButtonText();

        // 设置标准 SideSheet 中的操作按钮
        MaterialButton btnAction = standardSideSheet.findViewById(R.id.btnAction);
        btnAction.setOnClickListener(v -> {
            Toast.makeText(this, "标准 SideSheet 操作按钮被点击", Toast.LENGTH_SHORT).show();
            standardSideSheetBehavior.setState(SideSheetBehavior.STATE_HIDDEN);
        });

        // 设置模态 SideSheet 的显示按钮
        Button btnShowModal = findViewById(R.id.btnShowModal);
        btnShowModal.setOnClickListener(v -> showModalSideSheet());
    }

    private void toggleStandardSideSheet() {
        if (standardSideSheetBehavior.getState() == SideSheetBehavior.STATE_HIDDEN) {
            standardSideSheetBehavior.setState(SideSheetBehavior.STATE_EXPANDED);
        } else {
            standardSideSheetBehavior.setState(SideSheetBehavior.STATE_HIDDEN);
        }
    }

    private void updateToggleButtonText() {
        btnToggleStandard.setText(standardSideSheetBehavior.getState() == SideSheetBehavior.STATE_HIDDEN ?
                "显示标准 SideSheet" : "隐藏标准 SideSheet");
    }

    private void showModalSideSheet() {
        SideSheetDialog dialog = new SideSheetDialog(this);
        dialog.setContentView(R.layout.side_sheet_content);

        MaterialButton btnAction = dialog.findViewById(R.id.btnAction);
        if (btnAction != null) {
            btnAction.setOnClickListener(v -> {
                Toast.makeText(this, "模态 SideSheet 操作按钮被点击", Toast.LENGTH_SHORT).show();
                dialog.dismiss();
            });
        }

        dialog.show();
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            finish();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
} 